<?php

namespace App\Http\Controllers;

use App\Models\Project;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use App\Repositories\CompanyRepository;
use App\Repositories\ProjectRepository;
use App\Http\Requests\Logic\Project\QueryResource;
use App\Http\Requests\Logic\Project\StoreResource;
use App\Http\Requests\Logic\Project\UpdateResource;
use App\Http\Requests\Logic\Project\DestroyResource;
use App\Http\Requests\Logic\Project\QueryCollection;

/**
 * 项目管理控制器
 *
 * Date: 22/03/2018
 * @author George
 * @package App\Http\Controllers
 */
class ProjectController extends Controller
{
	protected $projectRepository;
	protected $companyRepository;

	/**
	 * ProjectController constructor.
	 * @param ProjectRepository $projectRepository
	 * @param CompanyRepository $companyRepository
	 */
	public function __construct(
		ProjectRepository $projectRepository,
		CompanyRepository $companyRepository
	)
	{
		$this->projectRepository = $projectRepository;
		$this->companyRepository = $companyRepository;
	}

	/**
	 * 获取项目数据
	 *
	 * Date: 11/04/2018
	 * @author George
	 * @param QueryCollection $request
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function index(QueryCollection $request)
	{
		$name = $request->get('name');
		$paginate = $request->get('paginate', 15);

		$query = $this->projectRepository->getByCompany(Auth::user()->company_id, $name);

		return success($query->paginate($paginate));
	}

	/**
	 * 获取指定项目详情
	 *
	 * Date: 11/04/2018
	 * @author George
	 * @param QueryResource $request
	 * @param Project $project
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function show(QueryResource $request, Project $project)
	{
		$date = $request->get('date');
		$start = $request->get('start');
		$end = $request->get('end');

		$query = $project->demands();

		$query->when($date, function ($query, $date) {
			/**
			 * @var Builder $query
			 */
			return $query->where('date', $date);
		});

		if ($start && $end) {
			$query->whereBetween('date', [$start, $end]);
		}

		$project->demands = $query->get();

		return success($project);
	}

	/**
	 * 创建项目
	 *
	 * Date: 11/04/2018
	 * @author George
	 * @param StoreResource $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \App\Exceptions\GeneralException
	 */
	public function store(StoreResource $request)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());
		$project = $this->projectRepository->create($attributes);
		return stored($project);
	}

	/**
	 * 更新项目
	 *
	 * Date: 11/04/2018
	 * @author George
	 * @param UpdateResource $request
	 * @param Project $project
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function update(UpdateResource $request, Project $project)
	{
		$attributes = $this->extractInputFromRules($request, $request->rules());
		$project->update($attributes);
		return updated($project);
	}

	/**
	 * 删除项目
	 *
	 * Date: 11/04/2018
	 * @author George
	 * @param DestroyResource $request
	 * @param Project $project
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Exception
	 */
	public function destroy(DestroyResource $request, Project $project)
	{
		$force = $request->get('force');
		$force ? $project->delete() : $project->forceDelete();
		return deleted();
	}
}
